﻿using Biz.Buffer;
using Biz.EF.Platform.SG;
using Hjt212Parser;
using log4net;
using System;
using System.Linq;
using System.Text;

namespace Biz.Process
{
    public static class Processor2011Alarm
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(Processor2011Alarm));
        private static StringBuilder AlarmMsg = new StringBuilder();
        public static void Process(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrWhiteSpace(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var port = CommonInformation.GetLetportByMn(mn);
                if (port == null) return;
                //var enterprise = CommonInformation.GetPollutionSourceByMn(mn);
                //if (enterprise == null) return;

                using (var pCtx = new PlatformXpfContext())
                {
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes == null) return;

                    Helper.RemoveMasterFactor(codes, true);

                    foreach (var code in codes)
                    {
                        var contam = CommonInformation.GetContamCode(code);
                        if (contam?.一级上限 == null || contam.一级上限 < 0.0000001) continue;
                        //if (contam.一级下限 == null) continue;

                        AlarmMsg.Clear();

                        var rtd = Parser.GetDoubleByKey(code + "-Rtd", content);
                        if (!rtd.HasValue) continue;

                        if (rtd.Value < contam.一级上限.Value) continue;

                        var record = new 报警记录();
                        record.企业基本信息ID = port.企业基本信息ID;
                        record.报警时间 = datatime;

                        var radio = (rtd.Value - contam.一级上限.Value) * 100 / contam.一级上限.Value;
                        record.内容 = datatime.Value.ToString("yyyy-MM-dd HH:mm:ss") + " " + contam.名称 + "浓度为 " + rtd.Value + " 超标 " + radio + "%";
                        if (radio > 100)
                        {
                            record.报警异常数据类型ID = 9;
                        }
                        else if (radio > 50)
                        {
                            record.报警异常数据类型ID = 8;
                        }
                        else if (radio > 30)
                        {
                            record.报警异常数据类型ID = 7;
                        }
                        else
                        {
                            record.报警异常数据类型ID = 3;
                        }
                        record.报警方式 = "SMS";
                        record.发送结果标志ID = 401;
                        record.是否发送 = false;
                        record.报警方式ID = 201;
                        record.企业排口ID = port.ID;
                        record.发送结果 = "未发送";
                        record.次数 = 0;
                        record.管理者分级代码 = "false";
                        pCtx.报警记录.Add(record);
                        pCtx.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                Log.Error(content);
            }
        }
    }
}
